//
//  CustomKeyBoardManger.m
//  WeCommunity
//
//  Created by mac on 2017/6/20.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "CustomKeyBoardManger.h"

static CustomKeyBoardManger * customKeyBoardManger = nil;

@implementation CustomKeyBoardManger

+(CustomKeyBoardManger *)shared
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        customKeyBoardManger = [[CustomKeyBoardManger alloc] init];
        
        [[NSNotificationCenter defaultCenter] addObserver:customKeyBoardManger selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification  object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:customKeyBoardManger selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
    });
    return customKeyBoardManger;
}

#pragma mark - 监听键盘的弹出
- (void)keyboardWillShow:(NSNotification *)note
{
    NSDictionary *info = [note userInfo];
    CGRect beginRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGRect endRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    //因为第三方键盘或者是在键盘加个toolbar会导致回调三次，这个判断用来判断是否是第三次回调，原生只有一次
    if(beginRect.size.height > 0 && (beginRect.origin.y - endRect.origin.y > 0)){
        //处理逻辑
        if (self.delegate) {
            [self.delegate keyboardIsShowAndBoardRect:endRect];
        }
    }
}
#pragma mark - 监听键盘的收回
- (void)keyboardWillHidden:(NSNotification *)note
{
    NSDictionary *info = [note userInfo];
    CGRect keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    if (self.delegate) {
        [self.delegate keyboardIsHiddenAndBoardRect:keyboardRect];
    }
    
}



@end
